﻿using System;
using System.Collections.Generic;

using E.Enum;
using E.BLL;
using E.Model;
using E.Web.UI.Page;

using X.Common;

namespace E.Web.Teacher.sys.notice
{
    public partial class Edit : TeacherBasePage
    {
        public IList<DictAreaModel> areaList;
        public IDictionary<int, string> categoryList;
        public NoticeModel model = new NoticeModel();
        public string act;
        public int id;
        public string area_v;

        protected override void ShowPage()
        {
            id = XRequest.GetInt("id", -1);
            act = XRequest.GetString("act");

            NoticeBLL bll = new NoticeBLL();
            categoryList = bll.GetNoticeCategoryList();

            if (ispost)
            {

                act = XRequest.GetString("act");
                string title = XRequest.GetString("TitleTxt");
                string content = XRequest.GetString("ContentTxt");
                int categoryid = XRequest.GetInt("CategorySlt", -1);
                int sysSelection = XRequest.GetInt("SysSlt", -1);
                string area = XRequest.GetString("area_v");
                string[] areas = GetArray(area);

                if (CheckValue())
                {
                    model = new NoticeModel();
                    model.Title = title;
                    model.CategoryID = categoryid;
                    model.IsSys = sysSelection == 1 ? true : false;
                    model.Content = content;
                    model.AreaStrList = areas;
                    model.CertificateID = authority == EnumTeacherAuthority.Internals ? (new TeacherBLL()).GetTeacherProjectID(teacherid) : -1;

                    int rst = 0;
                    string href = "";

                    switch (act)
                    {
                        case "n":
                            model.PublisherID = teacherid;
                            model.AddDate = DateTime.Now;

                            rst = bll.AddNotice(model);

                            href = "/sys/notice/Edit.aspx?act=n";

                            break;
                        case "u":
                            model.ID = id;

                            rst = bll.UpdateNotice(model);

                            href = "/sys/notice/Edit.aspx?act=u&id=" + id;
                            break;
                        default:
                            break;
                    }

                    if (rst > 0)
                    {
                        ExecuteJS("alert('操作成功'); window.location = '" + href + "';");
                    }
                    else
                    {
                        Alert("操作失败");
                    }
                }
            }
            else
            {
                areaList = GetTeacherAreaList(teacherid);
                model = bll.GetNoticeInfo(id);

                area_v = Convert2String(model.AreaIDList);
            }
        }

        private bool CheckValue()
        {
            bool rst = true;

            if (XRequest.GetString("TitleTxt").Equals(""))
            {
                Alert("标题不能为空");
                rst = false;
            }
            if (XRequest.GetString("ContentTxt").Equals(""))
            {
                Alert("内容不能为空");
                rst = false;
            }
            if (XRequest.GetInt("CategorySlt", -1) == -1)
            {
                Alert("请选择公告类别");
                rst = false;
            }
            if (XRequest.GetInt("SysSlt", -1) == -1)
            {
                Alert("请选择是否为系统公告");
                rst = false;
            }

            if (XRequest.GetString("area_v").Equals(""))
            {
                Alert("请选择至少一个地区");
                rst = false;
            }

            return rst;
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}
